#include "include.h"
#include "api.h"


#define BT_PROFILE          (PROF_A2DP*BT_A2DP_EN) | (PROF_HFP*BT_HFP_EN) | (PROF_SPP*BT_SPP_EN) | (PROF_HID*BT_HID_EN) | (PROF_PBAP*BT_PBAP_EN) | (PROF_MAP*BT_MAP_EN)
#define A2DP_FEATURE        (A2DP_VOL_CTRL*BT_A2DP_VOL_CTRL_EN) | (A2DP_RECON_CTRL*BT_A2DP_RECON_EN) | (A2DP_AVCTP_FLAG*BT_A2DP_AVCTP_EN) | (A2DP_AVDTP_DELAY_REPORT*BT_AVDTP_DELAY_REPORT_EN)
#define HFP_FEATURE         (HFP_BAT_REPORT*BT_HFP_BAT_REPORT_EN) | (HFP_IOS_RING*BT_HFP_PLAY_IOS_RING_EN) | (HFP_CALL_PRIVATE*BT_HFP_CALL_PRIVATE_FORCE_EN) | (HFP_SIRI_CMD*BT_HFP_SIRI_CMD_EN) |\
                            (HFP_EC_AND_NR*BT_HFP_EC_NR_EN)

bt_cfg_t bt_cfg = {
    .pwrup_connect_times = 3,  //BT_POWER_UP_RECONNECT_TIMES,
    .tout_connect_times =  20, //BT_TIME_OUT_RECONNECT_TIMES,
    .scan_timeout       = BT_DISCOVER_TIMEOUT,      //待实现
    .profile            = BT_PROFILE,
    .max_acl_link       = BT_2ACL_EN + 1,
    .a2dp_feature       = A2DP_FEATURE,
    .hfp_feature        = HFP_FEATURE,
    .vx_set             = 2,    //0=hv3, 1=ev3, 2=2ev3

    .res[0]             = 0,
    .res[1]             = 0,
    .dual_mode          = BT_DUAL_MODE_EN,
    .tws_mode           = BT_TWS_EN,
    .fcc_test_mode      = BT_FCC_TEST_EN,
    .cbt_test_mode      = BT_CBT_TEST_EN,
    .simple_pair_mode   = BT_SIMPLE_PAIR_EN,
    .scan_ctrl          = BT_DISCOVER_CTRL_EN,

    .sco_plc_en         = BT_PLC_EN,
    .sco_msbc_en        = BT_HFP_MSBC_EN,
};

rf_cfg_t rf_cfg;

const char bt_local_name[] = BT_NAME_DEFAULT;
const char bt_local_addr[6] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
const uint8_t bt_local_guid[16] = { 0x3C, 0x46, 0x71, 0x9D, 0xEF, 0xEC, 0x44, 0x03,
                                    0xA8, 0xAF, 0x88, 0x4E, 0x56, 0x89, 0x34, 0xFF
                                  };

/* user bt interface */
typedef struct {
    uint8_t mfi_uuid[16];
    uint8_t mfi_en;
    uint8_t device_status;
    uint32_t event;
} bsp_bt_mgr_t;

static bsp_bt_mgr_t g_bt_mgr = {
    .mfi_uuid = {0x00, 0x00, 0x00, 0x00, 0xDE, 0xCA, 0xFA, 0xDE, 0xDE, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xCA, 0xFE},
    .mfi_en = 0,
};

static int bsp_bt_mgr_server_class_is_inquiry(uint8_t *service_class, uint8_t len, uint8_t *pattern, uint8_t pattern_len)
{
    int ret = 0;
    if (len != pattern_len)
    {
        return ret;
    }

    if (memcmp(service_class, pattern, len) == 0)
    {
        ret = 1;
    }
    return ret;
}

static void bsp_bt_mgr_mfi_en(uint8_t en)
{
    g_bt_mgr.mfi_en = en;
}

int bsp_bt_mgr_mfi_status_get(void)
{
    return g_bt_mgr.mfi_en;
}

uint8_t *bsp_bt_mgr_mfi_uuid_get(void)
{
    return g_bt_mgr.mfi_uuid;
}

void bsp_bt_mgr_device_status_set(bsp_device_status_e sta)
{
    g_bt_mgr.device_status = sta;
}

uint8_t bsp_bt_mgr_device_status_get(void)
{
    return g_bt_mgr.device_status;
}

void bsp_bt_mgr_event_set(uint32_t evt)
{
    g_bt_mgr.event = evt;
}

uint32_t bsp_bt_mgr_event_get(void)
{
    return g_bt_mgr.event;
}

void bsp_bt_msg_handle(void)
{
    switch(g_bt_mgr.event)
    {
        case BT_NOTICE_INIT_FINISH:
            // bsp_bt_mgr_device_status_set(BSP_DEVICE_STATUS_ACTIVE);
            // iodm_log_printf("[ModuleLog] BT_NOTICE_INIT_FINISH");
            break;
        case BT_NOTICE_CONNECTED:
            iodm_protocol_frame_response(CMD_REPORT_CONNECT_RESULTS, 0, NULL);
            break;
        case BT_NOTICE_DISCONNECT:
            iodm_protocol_frame_response(CMD_REPORT_SPP_DISCONNECT, 0, NULL);
            iodm_protocol_frame_response(CMD_REPORT_DISCONNECT_RESULTS, 0, NULL);
            break;
    }

}


#if BT_SCO_FAR_DELAY_EN
u16 sco_far_pcm_2[0x17C0] AT(.sco_cache_2);
#endif

#if BT_2ACL_EN
AT(.text.stack.api)
bool btstack_disconnect_a2dp_playing(void)
{
    return sys_cb.bt_disconnect_a2dp_playing;
}
void bt_2acl_diconnect_playing(void)    //一拖二时，断开当前播放的设备连接，不断开第二台设备
{
    sys_cb.bt_disconnect_a2dp_playing = true;
    bt_disconnect();
    sys_cb.bt_disconnect_a2dp_playing = false;
}
AT(.text.stack.api)
bool btstack_set_scan_en(void)
{
    return sys_cb.bt_set_scan_en;
}
void bt_2acl_scan_en(void)              //一拖二时，连接第一台设备之后可以继续广播连接第二台
{
    sys_cb.bt_set_scan_en = true;
    bt_set_scan(1);
    sys_cb.bt_set_scan_en = false;
}
void bt_2acl_switch_music_play_next(void)              //一拖二时，在两台设备之间切换播放
{
    bt_2acl_switch_music_play();
    bt_music_play_pause();
}
#endif


//bool bt_sniff_featuer_disable(void)
//{
//    return true;  //打开此函数，可以关闭蓝牙sniff功能
//}


#if 0
//是否支持android & ios播放暂停快速切换，需要时定义该函数
//注意：打开后ios播放微信小视频会无声，播放器听音乐不影响
//      部分android4.0及之前系统打开多个播放器可能有兼容问题
bool bt_force_super_fast_status(void)
{
    return true;
}
#endif

#if 0
//是否支持ios播放暂停快速切换，需要时定义该函数，蓝牙后台建议打开
//注意：打开后ios播放微信小视频会无声，播放器听音乐不影响
bool bt_support_ios_fast_status(void)
{
    return true;
}
#endif

//是否支持BT AAC音频
bool bt_audio_aac_is_support(void)
{
    return BT_AAC_AUDIO_EN;
}

//不连接AVDTP 只连接AVCTP
//bool bt_a2dp_con_avctp_no_avdtp(void)
//{
//    return true;
//}

////tws配对后，是否只允许固定的主副箱连接
////需要重新配对时，调用bt_tws_delete_link_info()删除配对信息
//bool bt_tws_is_chk_link(void)
//{
//    return true;
//}

//是否强制设为tws slave不允许被手机连接
//bool bt_tws_slave_dis_scan(void)
//{
//    return true;
//}

//u8 bt_tws_get_force_role(void)
//{
//    if(xcfg_cb.bt_tws_lr_mode == 3) {
//        return sys_cb.tws_left_channel | 0x80; //0x80=master, 0x81=slave
//    }
//    return 0;
//}

//设置search slave次数，每次9s（搜索5秒 + 空闲4秒）
//uint8_t bt_tws_get_search_slave_times(void)
//{
//    return 3;
//}

//自定义蓝牙图标，根据需要打开
//u32 bt_get_class_of_device(void)
//{
//    return 0x240408;    //打开电话本功能，连接弹出允许连接窗口功能
//    return 0x002540;    //键盘图标，Android带显示电量，IOS不行。全部IOS均可连接HID拍照。
//    return 0x240418;    //耳机图标，Android和IOS均带电量显示。IOS9之前的机型无法连接HID拍照。（默认使用）
//}

//HID兼容性选择，根据需要选择
//u8 bt_hid_get_compatibility(void)
//{
//    return 0;   //耳机图标（带电量显示），不影响键盘使用，使用V+拍照，影响部分Android拍照，体验较好（默认使用）
//    //return 1;   //耳机图标（带电量显示），影响ios键盘使用，使用V+和ENTER拍照，拍照兼容性好
//    //return 2;   //键盘图标（IOS不带电量显示），影响ios键盘使用，使用V+和ENTER拍照，拍照兼容性好
//}

//PIN配对码（最多16个字符），默认"0000"
//#if !BT_SIMPLE_PAIR_EN
//const char *bt_get_pin_code(void)
//{
//    return "0000";
//}
//#endif

//远距离断线回连是否自动恢复播放，根据需要选择（默认是true）
//bool bt_reconn_paly(void)
//{
//    return false;
//}

//回连间隔（N+3000）mS，间隔越大，下一次回连越慢，更容易被其他手机搜索连接，N应大于等于2000
//u32 bt_reconn_time(void)
//{
//    return 2000;
//}

//是否需要一直回连手机
//bool bt_is_always_reconn(void)
//{
//    return false;
//}

//自定义回连方式，order为回连信息序号
//uint8_t connect_addr[6];
//void bt_cocnnect_order(uint8_t order)
//{
//    if(bt_nor_get_link_info_addr(connect_addr, order)) {
//        bt_send_msg(BT_MSG_CONNECT_ADDRESS);
//    }
//}
//
//uint8_t bt_get_connect_addr(uint8_t *bd_addr)
//{
//    memcpy(bd_addr, connect_addr, 6);
//    return 1;
//}

//#if (BT_TWS_CONN_TYPE == 1)
////主耳回连手机时间
//uint32_t bt_get_mst_connect_times(void)
//{
//    return 15;//(1.28s * n + 2)s
//}
//
////副耳回连主耳时间
//uint32_t bt_get_slv_connect_times(void)
//{
//    return 15;//(1.28s * n)s
//}
//#endif

/************************使用示例************************/
////按键发起回拨号码
//bt_send_msg(BT_MSG_CALL_REDIAL_NUMBER);
//delay_5ms(10); //延迟一下，等它发送完毕
//
////此函数返回需要回拨的号码，例如“10086”
//char * hfp_get_outgoing_number(void) {
//    return "10086";
//}

////发送自定义ATCMD
//bt_send_msg(BT_MSG_HFP_AT_CMD);
//delay_5ms(10); //延迟一下，等它发送完毕
//
// //此函数返回需要发送的ATCMD
#if BT_MAP_EN
char *hfp_get_at_cmd(void) {
    //    return "AT+VTS=1;"; //例如，通话过程发起号码键"1"
    //    return "ATD10086;"; //也可以，发起回拨号码"10086"
    return "AT+CCLK?\r";//获取IOS手机时间（安卓暂不支持），获取回调函数hfp_get_time
}

//获取IOS手机时间（安卓暂不支持）,需要先发送"AT+CCLK?\r"AT命令
void hfp_get_time(u8 *buf, u16 len)
{
    my_printf("hfp clk cmd callback:\n");
    //    print_r(buf, 16);
    bt_get_time((char *)buf, len, 1);
}
#endif
////tws时，单独连接/断开手机
//    if(bt_nor_is_connected()) {
//        bt_nor_disconnect();
//    } else {
//        bt_nor_connect();
//    }
////tws时，单独连接/断开对箱
//    if(bt_tws_is_connected()) {
//        bt_tws_disconnect();
//    } else {
//        bt_tws_search_slave();
//    }
/********************************************************/


//***********************************************************
//开启主从切换，蓝牙名字相同的情况下，禁止L跟L连接，R跟R连接
//注意需要固定左右声道
//bool tws_no_connect_same_side_en(void)
//{
//    return true;
//}
//u8 tws_no_connect_same_side(void)
//{
//    if(sys_cb.tws_left_channel){
//        return 1;
//    }else{
//        return 0;
//    }
//}
//**********************************************************


//u8 tws_key_compatible_with_old_versions(void)   //TWS兼容老版本(修正新SDK作副机,老版本作主机,副机上下曲控制无效问题)
//{
//    return 1;
//}

//void bt_inquiry_rssi_callback(uint8_t rssi)
//{
//    my_printf("rssi:%d\n",rssi);
//}

//bool bt_normal_inquiry(void){
//    return true;
//}
//
//bool bt_filte_remote_class_of_device(u8 *ptr)
//{
//    if(ptr[0] == 0x0c && ptr[1] == 0x02 && ptr[2] == 0x5a){
//        return true;
//    }else{
//        return false;
//    }
//}

/* enable spp numeric comparison */
bool bt_ssp_numeric_comparison(void)
{
    return true;
}
bool bt_ssp_auto_confirm(void)
{
    return true;
}

void bsp_bt_init(void)
{
    //更新配置工具的设置
    rf_cfg.txpwr   = xcfg_cb.bt_txpwr;
    rf_cfg.packet  = bt_get_packet(xcfg_cb.bt_rf_param);

    bt_cfg.profile = (PROF_A2DP * BT_A2DP_EN * xcfg_cb.bt_a2dp_en) | (PROF_HFP * BT_HFP_EN * xcfg_cb.bt_sco_en)\
                     | (PROF_SPP * BT_SPP_EN * (xcfg_cb.bt_spp_en || xcfg_cb.eq_dgb_spp_en)) | (PROF_HID * BT_HID_EN * xcfg_cb.bt_hid_en)\
                     | (PROF_PBAP * BT_PBAP_EN) | (PROF_MAP * BT_MAP_EN);
    bt_cfg.a2dp_feature = (A2DP_VOL_CTRL * BT_A2DP_VOL_CTRL_EN * xcfg_cb.bt_a2dp_vol_ctrl_en) | (A2DP_RECON_CTRL * BT_A2DP_RECON_EN)\
                          | (A2DP_AVCTP_FLAG * BT_A2DP_AVCTP_EN) | (A2DP_AVDTP_DELAY_REPORT * BT_AVDTP_DELAY_REPORT_EN);
    bt_cfg.dual_mode = BT_DUAL_MODE_EN * xcfg_cb.ble_en;
    bt_cfg.max_acl_link = BT_2ACL_EN * xcfg_cb.bt_2acl_en + 1;
    bt_cfg.pwrup_connect_times = BT_POWER_UP_RECONNECT_TIMES;
    bt_cfg.tout_connect_times = BT_TIME_OUT_RECONNECT_TIMES;
#if BT_TWS_EN
    bt_cfg.tws_mode = BT_TWS_EN * xcfg_cb.bt_tws_en;
    if (xcfg_cb.bt_tws_pair_mode != 0) {
        bt_cfg.res[0] |= BT_FLAG0_TWS_SCAN;
        if (xcfg_cb.bt_tws_pair_mode == 3) {
            bt_cfg.res[0] |= BT_FLAG0_TWS_MENU_PAIR;
        }
    }
    if (bt_cfg.tws_mode && get_cur_sysclk() >= SYS_48M) {
        bt_cfg.res[0] |= BT_FLAG0_CLK_ADVANCE;
    }
    if (xcfg_cb.bt_tws_lr_mode == 3) {
        tws_lr_xcfg_sel();
    }
#endif

    if (xcfg_cb.bt_rf_ltx_recon != 0) {
        bt_cfg.res[1] |= BT_FLAG1_LTX_RECON;
    }

#if BT_FCC_TEST_EN
    bt_fcc_init();
#endif

#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        bt_cfg.profile = PROF_HID;
        bt_cfg.max_acl_link = 1;
        bt_cfg.dual_mode = 0;
        bt_cfg.tws_mode = 0;
    }
#endif // FUNC_BTHID_EN

    memset(&f_bt, 0, sizeof(func_bt_t));
    f_bt.disp_status = 0xfe;
    f_bt.need_pairing = 1;  //开机若回连不成功，需要播报pairing
    if (!is_bthid_mode()) {
        f_bt.hid_menu_flag = 1;
    }

    bt_setup();
    // bt_off();
#if BT_SCO_FAR_DELAY_EN
    bt_set_sco_far_delay(sco_far_pcm_2, sizeof(sco_far_pcm_2) / sizeof(sco_far_pcm_2[0]), 0x1000);
#endif
#if BT_SDP_SEARCH_UUID_EN
    bt_sdp_search_init();
#endif
}

void bsp_bt_close(void)
{
}

#if BT_PWRKEY_5S_DISCOVER_EN
bool bsp_bt_w4_connect(void)
{
#if 0
    if (xcfg_cb.bt_pwrkey_nsec_discover) 
#else
#endif
    {
        while (sys_cb.pwrkey_5s_check) {            //等待检测结束
            WDT_CLR();
            delay_5ms(2);
        }

        //已检测到长按5S，需要直接进入配对状态。播放PAIRING提示音。
        if (sys_cb.pwrkey_5s_flag) {
            return false;
        }
    }
    return true;
}

bool bsp_bt_pwrkey5s_check(void)
{
    bool res = !bsp_bt_w4_connect();
    delay_5ms(2);
    return res;
}

void bsp_bt_pwrkey5s_clr(void)
{
#if 0
    if (!xcfg_cb.bt_pwrkey_nsec_discover) {
        return;
    }
#endif
    sys_cb.pwrkey_5s_flag = 0;
#if !BT_DISCOVER_CTRL_EN    //使用按键打开可被发现时scan_ctrl=1
    bt_cfg.scan_ctrl = 0;   //scan_ctrl=0，则连接断开后会自动打开可被发现
#endif
}
#endif // BT_PWRKEY_5S_DISCOVER_EN

void bsp_bt_vol_change(void)
{
#if BT_A2DP_VOL_CTRL_EN || BT_TWS_EN
    if ((xcfg_cb.bt_a2dp_vol_ctrl_en && (bt_get_status() >= BT_STA_CONNECTED)) || bt_tws_is_connected()) {
        bt_send_msg(BT_MSG_VOL_CHANGE);     //通知手机音量已调整
    }
#endif
}

#if BT_HID_MENU_EN
void bsp_bt_hid_tog_conn(void)
{
    if (xcfg_cb.bt_hid_menu_en) {
        func_bt_chkclr_warning(BT_WARN_HID_CON | BT_WARN_HID_DISCON);
        f_bt.hid_menu_flag = 2;     //按键连接/断开HID，置上标志，用于播放提示音
        if (bt_hid_is_connected()) {
            bt_hid_disconnect();
        } else {
            bt_hid_connect();
        }
    }
}
#endif

void bsp_bt_hid_photo(void)
{
    bt_hid_key(HID_KEY_ENTER);              //enter key, android 4.0以上
    delay_5ms(10);
    bt_hid_consumer(HID_KEY_VOL_UP);        //consumer key vol_up, ios
}

/*****************************************************************************
 * 回调函数
 *****************************************************************************/
uint a2dp_get_vol(void)
{
    uint vol;

    vol = ((u32)sys_cb.vol * 1280L / VOL_MAX) / 10;
    if (vol > 0x7f) {
        vol = 0x7f;
    }

    return vol;
}

uint hfp_get_mic_gain(void)
{
    return 9;
}

uint hfp_get_spk_gain(void)
{
    return sys_cb.hfp_vol;
}

uint hfp_get_bat_level(void)
{
#if VBAT_DETECT_EN
    //计算方法：level = (实测电压 - 关机电压) / ((满电电压 - 关机电压) / 10)
    u16 bat_off = LPWR_OFF_VBAT * 100 + 2700;
    if (bat_off > sys_cb.vbat) {
        return 0;
    }
    uint bat_level = (sys_cb.vbat - bat_off) / ((4200 - bat_off) / 10);
    if (bat_level) {
        bat_level--;
    }
    //    my_printf("bat level: %d %d\n", sys_cb.vbat, bat_level);
    return bat_level;
#else
    return 9;
#endif
}

//tws一些同步信息，例如EQ, 语言等，param最大是12byte
bool bt_get_tws_info(uint8_t *param)
{
#if LANG_SELECT == LANG_EN_ZH
    param[0] = sys_cb.lang_id & 0x0f;  //bit0~3
#endif
#if EQ_MODE_EN
    param[0] |= (u8)sys_cb.eq_mode << 4;
#endif
    param[1] = sys_cb.hfp_vol;      //同步初始通话音量

#if BT_TSCO_EN
    if (f_bt.disp_status == BT_STA_INCOMING) {
        bt_ring_get_tick(param + 2); //byte[2:6]
    }
#endif
    return true;
}

void bt_set_tws_info(uint8_t *param)
{
    uint8_t tmp = 0;
#if LANG_SELECT == LANG_EN_ZH
    tmp = (param[0] & 0x0f);
    if (xcfg_cb.lang_id >= 2 && tmp < 2 && tmp != sys_cb.lang_id) { //bit0~3
        sys_cb.lang_id = tmp;
        msg_enqueue(EVT_BT_SET_LANG_ID);
    }
#endif
#if EQ_MODE_EN
    tmp = (param[0] & 0xf0) >> 4;
    if (tmp < 6 && tmp != sys_cb.eq_mode) { //bit4~7
        sys_cb.eq_mode = tmp;
        msg_enqueue(EVT_BT_SET_EQ);
    }
#endif
    sys_cb.hfp_vol = param[1];      //同步初始通话音量

#if BT_TSCO_EN
    if (f_bt.disp_status == BT_STA_INCOMING) {
        bt_ring_set_tick(param + 2); //byte[2:6]
    }
#endif
    tmp = tmp;      //避免编译警告
}

void tbox_notice(u8 *param);
void bt_emit_notice(uint evt, u32 param)
{
    my_printf("%s event:%d\n", __func__, evt);
    switch (evt) {
        case BT_NOTICE_INIT_FINISH:
#if CHARGE_DC_RESET_DELAY && BT_TWS_AUTO_SWITCH
            if (CHARGE_DC_RESET || BT_TWS_AUTO_SWITCH) {
                sys_cb.dc_rst_flag = 1;
                RTCCON &= ~BIT(6);
            }
#endif
#if 0
#if BT_PWRKEY_5S_DISCOVER_EN
            if (bsp_bt_w4_connect()) {
                bt_connect();
            } else {
                bt_cfg.scan_ctrl = 1;
                bt_set_scan(true);          //长按5S开机，直接进bt scanning
            }
#else
            bt_connect();
#endif
#endif
            bsp_bt_mgr_event_set(evt);
            msg_enqueue(EVT_BT_EMIT_NOTICE);
            break;

        case BT_NOTICE_TWS_RES_PLAY:
            sys_cb.tws_res_brk = 1;    //打断当前提示音
            tws_res_add(*(u8 *)param, *(u32 *)(param + 1));
            break;
        case BT_NOTICE_DISCONNECT:
#if BT_TWS_EN
            if (bt_cfg.tws_mode) {
                if (param & 0xc0) {
                    f_bt.tws_status &= ~0xc0;
                    f_bt.warning_status |= BT_WARN_TWS_DISCON;
                    break;                      //tws断线不播报提示音
                } else {
                    f_bt.tws_status &= ~0x01;
                }
            }
#endif
            bsp_bt_mgr_mfi_en(0);
            bsp_bt_mgr_event_set(evt);
            msg_enqueue(EVT_BT_EMIT_NOTICE);
            // iodm_protocol_frame_response(CMD_REPORT_SPP_DISCONNECT, 0, NULL);
            // iodm_protocol_frame_response(CMD_REPORT_DISCONNECT_RESULTS, 0, NULL);
            f_bt.warning_status |= BT_WARN_DISCON;
            bt_redial_reset(((u8 *)param)[0] & 0x01);
            delay_5ms(5);
            break;
        case BT_NOTICE_CONNECTED:
            //bit7: tws slave
            //bit6: tws master
            //bit5: be connected
            //bit4: first connect
#if BT_TWS_EN
            if (bt_cfg.tws_mode) {
                if (param & 0x80) {
                    f_bt.tws_status |= 0x80;
                    f_bt.warning_status |= BT_WARN_TWS_SCON;
                } else if (param & 0x40) {
                    f_bt.tws_status |= 0x40;
                    f_bt.warning_status |= BT_WARN_TWS_MCON;
                } else {
                    f_bt.tws_status |= 0x01;
                    f_bt.warning_status |= BT_WARN_CON;
                }
            } else
#endif
            {
                f_bt.warning_status |= BT_WARN_CON;
                bt_redial_reset(((u8 *)param)[0] & 0x01);
            }
#if BT_PWRKEY_5S_DISCOVER_EN
            bsp_bt_pwrkey5s_clr();
#endif // BT_PWRKEY_5S_DISCOVER_EN

#if 1
            bt_search_uuid_list();
            bt_spp_connect_and_disconnect();
#endif
            bsp_bt_mgr_event_set(evt);
            msg_enqueue(EVT_BT_EMIT_NOTICE);
            // iodm_protocol_frame_response(CMD_REPORT_CONNECT_RESULTS, 0, NULL);
            delay_5ms(5);
            break;
        case BT_NOTICE_LOSTCONNECT:
        case BT_NOTICE_INCOMING:
        case BT_NOTICE_RING:
        case BT_NOTICE_OUTGOING:
        case BT_NOTICE_CALL:
            break;
        case BT_NOTICE_SET_SPK_GAIN:
            if (param != sys_cb.hfp_vol) {
                sys_cb.hfp_vol = param;
                msg_enqueue(EVT_HFP_SET_VOL);
            }
            break;

        case BT_NOTICE_MUSIC_PLAY:
            msg_enqueue(EVT_A2DP_MUSIC_PLAY);
            break;

        case BT_NOTICE_MUSIC_STOP:
            if (bt_get_disp_status() > BT_STA_PLAYING) {
                break;
            }
            msg_enqueue(EVT_A2DP_MUSIC_STOP);
            break;

        case BT_NOTICE_MUSIC_CHANGE_VOL:
            if (param == 0) {
                msg_enqueue(KU_VOL_DOWN);
            } else {
                msg_enqueue(KU_VOL_UP);
            }
            break;

        case BT_NOTICE_TWS_HID_SHUTTER:
            msg_enqueue(EVT_TWS_HID_SHUTTER);
            break;

        case BT_NOTICE_TWS_INFO:
            bt_set_tws_info((u8 *)param);
            break;

        case BT_NOTICE_TWS_SET_VOL:
            param = (param + 1) * VOL_MAX / 128;
            sys_cb.vol = param;
            if (sys_cb.vol > VOL_MAX) {
                sys_cb.vol = VOL_MAX;
            }
            msg_enqueue(EVT_TWS_SET_VOL);
            break;

        case BT_NOTICE_MUSIC_SET_VOL:
            param = (param + 1) * VOL_MAX / 128;
            if (param != sys_cb.vol) {
                sys_cb.vol = param;
                if (sys_cb.vol > VOL_MAX) {
                    sys_cb.vol = VOL_MAX;
                }
                msg_enqueue(EVT_A2DP_SET_VOL);
            }
            break;
        case BT_NOTICE_HID_CONN_EVT:
#if BT_HID_MENU_EN
            if (xcfg_cb.bt_hid_menu_en) {
                if (f_bt.hid_menu_flag == 2) {
                    //按键连接/断开HID Profile完成
                    if (param) {
                        f_bt.warning_status |= BT_WARN_HID_CON;
                    } else {
                        f_bt.warning_status |= BT_WARN_HID_DISCON;
                    }
                    f_bt.hid_menu_flag = 1;
                }
#if BT_HID_DISCON_DEFAULT_EN
                else if (f_bt.hid_menu_flag == 1) {
                    if ((param) & (xcfg_cb.bt_hid_discon_default_en)) {
                        f_bt.hid_discon_flag = 1;
                    }
                }
#endif // BT_HID_DISCON_DEFAULT_EN
            }
#endif // BT_HID_MENU_EN
#if BT_HID_MOBILE_REMOTE_CONTROL
            if (param) {
                msg_enqueue(EVT_HID_DATA_REPORT);
            }
#endif
            break;
        case BT_NOTICE_BT_INFO:
            //返回 数据形式 (u8 *)param[0] len （地址+蓝牙名字）
            //        (u8 *)param[1-6] bt addr
            //        (u8 *)param[7~] bt name
            my_printf("BT_NOTICE_BT_INFO ");
            //        print_r((u8 *)param, 40);

#if 1
            u8 *ptr = (u8 *)param;
            my_printf("mac:");
            for (int i = 0; i < 6; i++)
            {
                my_printf("%02X", ptr[i + 1]);
            }
            my_printf("\n");

            my_printf("name: %s \n", ptr + 7);
#endif
            break;
        case BT_NOTICE_INQUIRY_FINISH:
            break;
        case BT_NOTICE_CONNECT_STATUS:
            my_printf("BT_NOTICE_CONNECT_STATUS = 0x%X\n", ((u8 *)param)[0]);
            break;
        case BT_NOTICE_DISCONNECT_STATUS:
            my_printf("disconnect sta = %d\n", ((u8 *)param)[0]);
            break;
        case BT_NOTICE_PULLPHONEBOOK_FINISH:
            break;
        case BT_NOTICE_HFP_INDICATOR_STATUS:
            //param[0~7]  status
            //param[8~15] ind 1->battchg  2->signal
            break;
        case BT_NOTICE_SREVICE_CLASS: {
            //  UUID  格式 说明
            //  byte[0] len, byte[1] 以下是数据，长度小于16的UUID  是2BYTE， 长度是16的，就是自定义128BIT UUID
            //        my_printf("BT_NOTICE_SREVICE_CLASS\n");
            //        u8 *ptr = (u8*)param;
            //        print_r(ptr,ptr[0]+1);

#if 1
            u8 *ptr = (u8 *)param;
            // iodm_protocol_frame_response(CMD_REPORT_SERVER_CLASS, ptr[0], &ptr[1]);
            int ret = bsp_bt_mgr_server_class_is_inquiry(&ptr[1], ptr[0], g_bt_mgr.mfi_uuid, sizeof(g_bt_mgr.mfi_uuid));
            if (ret)
            {
                bsp_bt_mgr_mfi_en(1);
            }

            my_printf("BT_NOTICE_SREVICE_CLASS\n");
            my_printf("class uuid :");
            for (int i = 0; i < ptr[0]; i++)
            {
                my_printf(" %02X", ptr[i + 1]);
            }
            my_printf("\n");
            break;
#endif
        }
        case BT_NOTICE_SSP_NUMERIC_VALUE:
            my_printf("param:%d\n", param);
            bt_ssp_numeric_comparison_res(1);  //配对应答，1：同意，0：拒绝
            break;

        case BT_NOTICE_RECON_FINISH:          //bt_nor_connect连接手机失败notice 状态
            my_printf("BT_NOTICE_RECON_FINISH\n");
            my_printf("reason:%x\n", param);
            break;
        case BT_NOTICE_PROFILE:
            my_printf("BT_NOTICE_PROFILE param:%d\n", param);
            break;
    }
}

uint8_t bt_get_func_sta(void)
{
    if (func_cb.sta == FUNC_PWROFF) {
        return FUNC_PWROFF;
    }
    return 0xff;
}

#if IODM_TEST_MODE
void bt_save_qr_addr(u8 *addr)
{
    cm_write8(PARAM_QR_ADDR_VALID, 1);
    cm_write(addr, PARAM_QR_ADDR, 6);
    cm_sync();
}

bool bt_get_qr_addr(u8 *addr)
{
    if (cm_read8(PARAM_QR_ADDR_VALID)) {
        cm_read(addr, PARAM_QR_ADDR, 6);
        return true;
    }
    return false;
}

void bt_save_new_name(char *name, u8 len)
{
    cm_write8(PARAM_BT_NAME_LEN, len);
    cm_write(name, PARAM_BT_NAME, len);
    cm_sync();
}

bool bt_get_new_name(char *name)
{
    u8 len = cm_read8(PARAM_BT_NAME_LEN);
    if (len > 32 || len == 0) {
        return false;
    }
    memset(name, 0x00, 32); //clear
    cm_read(name, PARAM_BT_NAME, len);
    return true;
}
#endif // IODM_TEST_MODE

bool bt_set_func_sta(uint8_t func_sta)
{
    if (func_sta != 0xff) {
        sys_cb.pwrdwn_tone_en = 1;
        func_cb.sta = FUNC_PWROFF;
    }
    return true;
}

const char *bt_get_local_name(void)
{
#if 1
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        return xcfg_cb.bthid_name;
    }
#endif // FUNC_BTHID_EN

#if IODM_TEST_MODE
    bt_get_new_name(xcfg_cb.bt_name);
#endif // IODM_TEST_MODE

    return xcfg_cb.bt_name;
#else
    return bt_local_name;
#endif // 1

}

void bt_save_master_addr(u8 *addr)
{
    if (BT_TWS_AUTO_SWITCH) {
        cm_write8(PARAM_MASTER_ADDR_VALID, 1);
        cm_write(addr, PARAM_MASTER_ADDR, 6);
        cm_sync();
    }
}

bool bt_get_master_addr(u8 *addr)
{
    if (BT_TWS_AUTO_SWITCH && cm_read8(PARAM_MASTER_ADDR_VALID)) {
        cm_read(addr, PARAM_MASTER_ADDR, 6);
        return true;
    }
    return false;
}

void bt_get_local_bd_addr(u8 *addr)
{
    if (bt_get_master_addr(addr)) {
        return;
    }
#if BT_LOCAL_ADDR
    param_random_key_read(&addr[2]);
    addr[0] = 0x41;
    addr[1] = 0x42;
#else
    memcpy(addr, xcfg_cb.bt_addr, 6);
#endif

#if IODM_TEST_MODE
    if (bt_get_qr_addr(addr)) {
        return;
    }
#endif // IODM_TEST_MODE

#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        addr[5] ^= BIT(0);
    }
#endif // FUNC_BTHID_EN
}

u32 bt_get_rand_seed(void)
{
    return sys_cb.rand_seed;
}

WEAK const uint8_t *bt_get_rf_param(void)
{
    return NULL;
}

void bt_get_guid(uint8_t *buf, uint8_t buf_len)
{
    const char *ptr = bt_get_local_name();
    u16 name_len = strlen(ptr);
    if (name_len > 10) {
        name_len = 10;
    }
    if (name_len > buf_len) {
        name_len = buf_len;
    }
    memcpy(buf, bt_local_guid, buf_len);
    memcpy(buf, ptr, name_len);
}

//左右耳不同名字匹配，例如TWS-L, TWS-R
//名字长度超过10个字符时，需要把bt_get_guid的if(name_len > 10)限制去掉
//bool bt_get_remote_guid(uint8_t *buf, uint8_t buf_len)
//{
//    const char *ptr = bt_get_local_name();
//    u16 name_len = strlen(ptr);
//    if(name_len > buf_len) {
//        name_len = buf_len;
//    }
//    bt_get_guid(buf, buf_len);
//    if(buf[name_len-1] == 'L') {
//        buf[name_len-1] = 'R';
//    } else if(buf[name_len-1] == 'R') {
//        buf[name_len-1] = 'L';
//    }
//    return true;
//}

#if BT_SUPPORT_EIGHT_DEVICE_EN
uint8_t is_support_eight_device(void)
{
    return true;
}
#endif // BT_SUPPORT_EIGHT_DEVICE_EN

void bt_get_link_info(void *buf, u16 addr, u16 size)
{
    //my_printf("bt_read: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        cm_read(buf, BTHID_CM_PAGE(addr), size);
    } else
#endif // FUNC_BTHID_EN

#if BT_SUPPORT_EIGHT_DEVICE_EN
        if ((addr + size) >= 256) {
            u16 addr_temp = 0;
            addr = addr + size - 256;
            if (addr < size) {
                addr_temp = 0;
            }
            for (; addr > size; addr -= size) {
                addr_temp += size;
            }
            cm_read(buf, BT_CM_PAGE_2(addr_temp), size);
        } else
#endif // BT_SUPPORT_EIGHT_DEVICE_EN
        {
            cm_read(buf, BT_CM_PAGE(addr), size);
        }
    //print_r(buf, size);
}

void bt_put_link_info(void *buf, u16 addr, u16 size)
{
    //my_printf("bt_write: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    //print_r(buf, size);
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        cm_write(buf, BTHID_CM_PAGE(addr), size);
    } else
#endif // FUNC_BTHID_EN

#if BT_SUPPORT_EIGHT_DEVICE_EN
        if ((addr + size) >= 256) {
            u16 addr_temp = 0;
            addr = addr + size - 256;
            if (addr < size) {
                addr_temp = 0;
            }
            for (; addr > size; addr -= size) {
                addr_temp += size;
            }
            cm_write(buf, BT_CM_PAGE_2(addr_temp), size);
        } else
#endif // BT_SUPPORT_EIGHT_DEVICE_EN
        {
            cm_write(buf, BT_CM_PAGE(addr), size);
        }
}

void bt_get_ext_link_info(void *buf, u16 addr, u16 size)
{
    //my_printf("bt_read: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    cm_read(buf, EXT_CM_PAGE(addr), size);
    //print_r(buf, size);
}

void bt_put_ext_link_info(void *buf, u16 addr, u16 size)
{
    //my_printf("bt_write: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    //print_r(buf, size);
    cm_write(buf, EXT_CM_PAGE(addr), size);
}

void bt_sync_link_info(void)
{
    cm_sync();
}



//android auto uuid
const uint8_t spp_service_buffer_user[] =
{
    //private static final UUID MY_UUID_INSECURE =UUID.fromString("4de17a00-52cb-11e6-bdf4-0800200c9a66");
    0x36, 0x00, 0x4e, 0x09, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00, 0x0a, 0x09,
    0x00, 0x01, 0x35, 0x11, 0x1c,
    0x4d, 0xe1, 0x7a, 0x00, 0x52, 0xcb, 0x11, 0xe6, 0xbd, 0xf4, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66,
    0x09, 0x00, 0x02, 0x0a, 0x00, 0x00, 0x7f, 0xde, 0x09, 0x00, 0x04,
    0x35, 0x0c, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x01, 0x09, 0x00,
    0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x01, 0x09, 0x01, 0x02, 0x09, 0x01, 0x00, 0x25, 0x05,
    0x53, 0x70, 0x70, 0x31, 0x00
};

//ios
const uint8_t spp_service_buffer_user_ios[] =
{
    0x36, 0x00, 0x66, 0x09, 0x00, 0x00, 0x0A, 0x00, 0x01, 0x00, 0x06, 0x09, 0x00,
    0x01, 0x35, 0x11, 0x1C, 0x00, 0x00, 0x00, 0x00, 0xDE, 0xCA, 0xFA, 0xDE,
    0xDE, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xCA, 0xFF, 0x09, 0x00, 0x04, 0x35,
    0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08,
    0x04, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06,
    0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09,
    0x00, 0x08, 0x08, 0xFF, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19,
    0x11, 0x01, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x0A, 0x43, 0x75,
    0x73, 0x74, 0x6F, 0x6D, 0x65, 0x72, 0x63, 0x00
};


uint8_t sdp_register_service(const uint8_t *record);
void sdp_register_service_init(void)
{
    sdp_register_service(spp_service_buffer_user);
    sdp_register_service(spp_service_buffer_user_ios);
}


//设置 EIR UUID
/* accessory carplay uuid over EIR */
u8 bt_inquiry_res_eir_uuid(u8 *prt)
{
    u8 uuid[16] = {0xFF, 0xCA, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xDE, 0xDE, 0xFA, 0xCA, 0xDE, 0x00, 0x00, 0x00, 0x00};
    //  u8 uuid1[16] = {0xEC, 0x88, 0x43, 0x48, 0xCD, 0x41, 0x40, 0xA2, 0x97, 0x27, 0x57, 0x5D, 0x50, 0xBF, 0x1F, 0xD3};    // 大小端区别
    u8 uuid1[16] = {0xD3, 0x1F, 0xBF, 0x50, 0x5D, 0x57, 0x27, 0x97, 0xA2, 0x40, 0x41, 0xCD, 0x48, 0x43, 0x88, 0xEC};
    memcpy(prt, uuid, 16);
    memcpy(prt + 16, uuid1, 16);
    return 32;
}

bool bt_spp_user_uuid_en(void)
{
    return true;
}
const u8 user_search_uuid128[16] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00,  0x10, 0x00,  0x80, 0x00,  0x00, 0x80, 0x5f, 0x9b, 0x34, 0xFb}; ///test uuid
const u8 uuid_mfi[16] = {0x00, 0x00, 0x00, 0x00, 0xDE, 0xCA, 0xFA, 0xDE, 0xDE, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xCA, 0xFE};
//发起SPP连接之前先修改以下函数UUID，即可按此UUID 连接
void bt_spp_user_uuid(u8 *uuid)
{
    memcpy(uuid, uuid_mfi, 16);
}


#if BT_SDP_SEARCH_UUID_EN
#define SERVICE_RECORD_COUNT_MAX           32
u32 record_handle[SERVICE_RECORD_COUNT_MAX];
u8 record_handle_list[SERVICE_RECORD_COUNT_MAX * 16];
u8 record_pattern[SERVICE_RECORD_COUNT_MAX * 12];
u8 service_class[18];
void bt_sdp_search_init(void)
{
    bt_sdp_search_init_do(record_handle_list, record_pattern, record_handle, service_class, SERVICE_RECORD_COUNT_MAX);
}
#endif


#if BT_FCC_TEST_EN
ALIGNED(4)
AT(.fcc_buf.huart)
u8 huart_buffer[512];

void huart_init(void)
{
    huart_init_do(HUART_TR_PB3, HUART_TR_PB3, 1500000, huart_buffer, 512);
}
#elif BT_BQB_TEST_EN
ALIGNED(4)
AT(.fcc_buf.huart)
u8 huart_buffer[512];

void huart_init(void)
{
    huart_init_do(HUART_TR_PB1, HUART_TR_PB2, 9600, huart_buffer, 512);
}
#endif

#if BT_TOUCH_SCREEN_EN
struct ts_ctl_t ts_ctl;
void hid_data_send(void)
{
    ts_ctl.act = 1;  //触摸按下是1，松开是0
    ts_ctl.x = 0x6400; //改变坐标位置
    ts_ctl.y = 0x7902;
    bt_hid_touch_screen_set_key(&ts_ctl);
    delay_5ms(10);
    ts_ctl.act = 1;  //触摸按下是1，松开是0
    ts_ctl.x = 0x6400;
    ts_ctl.y = 0x7e02;
    bt_hid_touch_screen_set_key(&ts_ctl);
    delay_5ms(10);
    ts_ctl.act = 1;  //触摸按下是1，松开是0
    ts_ctl.x = 0x5f00;
    ts_ctl.y = 0x7e02;
    bt_hid_touch_screen_set_key(&ts_ctl);
    delay_5ms(10);
    ts_ctl.act = 0;  //松开0
    ts_ctl.x = 0x5f00;
    ts_ctl.y = 0x7e02;
    bt_hid_touch_screen_set_key(&ts_ctl);
}

//bt_hid_consumer(0x01);  //按键用这个函数发HID信息
#endif

#if BT_HID_MOUSE_EN
static int16_t screen_x_max;
static int16_t screen_y_max;
static hid_mouse_action_t mouse_action_para;

u8 bt_hid_get_compatibility(void)
{
    return 6;
}

//每次触摸屏幕动作结束后，重置光标到初始位置
void bt_hid_reset_location_of_mouse_pointer(void)
{
    memset(&mouse_action_para, 0x00, sizeof(hid_mouse_action_t));

    mouse_action_para.x_diff = screen_x_max;
    mouse_action_para.y_diff = screen_y_max;
    bt_hid_mouse_action_send(&mouse_action_para);
}

//para: max_x, max_y 分别表示手机屏幕的横向,纵向分辨率
//e.g. samsung NOTE 10 的 max_x 为 1080, max_y 为 2280
//HID 连接上后调用一次，将光标重置到初始位置
void bt_hid_mouse_screen_init(int16_t max_x, int16_t max_y)
{
    screen_x_max = max_x;
    screen_y_max = max_y;

    bt_hid_reset_location_of_mouse_pointer();
}

//demo
void bt_hid_mouse_swipe_simulate_demo(bool dir)
{
    memset(&mouse_action_para, 0x00, sizeof(hid_mouse_action_t));

    //set left button triggered event
    mouse_action_para.left_click = 1;
    if (dir) {
        //点击屏幕的坐标（实际位置减去初始位置得到的变化量）
        mouse_action_para.x_diff = -500;
        mouse_action_para.y_diff = -1000;
        bt_hid_mouse_action_send(&mouse_action_para);
        delay_5ms(4);

        //模拟向下滑动屏幕
        mouse_action_para.x_diff = 0;
        mouse_action_para.y_diff = 50;

    } else {
        mouse_action_para.x_diff = -500;
        mouse_action_para.y_diff = -1000;
        bt_hid_mouse_action_send(&mouse_action_para);
        delay_5ms(4);

        //模拟向上滑动屏幕
        mouse_action_para.x_diff = 0;
        mouse_action_para.y_diff = -50;
    }

    //上报坐标变化量
    for (int i = 0; i < 5; ++i) {
        bt_hid_mouse_action_send(&mouse_action_para);
        delay_5ms(4);
    }

    //松开按键
    mouse_action_para.left_click = 0;
    mouse_action_para.x_diff = 0;
    mouse_action_para.y_diff = 0;
    bt_hid_mouse_action_send(&mouse_action_para);
    delay_5ms(1);

    //重置光标回屏幕的右下角（screen_x_max，screen_y_max ）
    bt_hid_reset_location_of_mouse_pointer();
}

void bt_hid_mouse_click_simulate_demo(void)
{
    //reset the pointer position
    memset(&mouse_action_para, 0x00, sizeof(hid_mouse_action_t));
    //first touch position
    mouse_action_para.left_click = 1;
    mouse_action_para.x_diff = -540;
    mouse_action_para.y_diff = -1000;
    bt_hid_mouse_action_send(&mouse_action_para);
    delay_5ms(10);

    mouse_action_para.left_click = 0;
    mouse_action_para.x_diff = 0;
    mouse_action_para.y_diff = 0;
    bt_hid_mouse_action_send(&mouse_action_para);
    delay_5ms(30);

    mouse_action_para.left_click = 1;
    mouse_action_para.x_diff = 0;
    mouse_action_para.y_diff = 0;
    bt_hid_mouse_action_send(&mouse_action_para);
    delay_5ms(10);

    //position when release
    mouse_action_para.left_click = 0;
    mouse_action_para.x_diff = 0;
    mouse_action_para.y_diff = 0;
    bt_hid_mouse_action_send(&mouse_action_para);
    delay_5ms(4);

    //reset position of mouse
    bt_hid_reset_location_of_mouse_pointer();
}
#endif


#if BT_RF_EXT_CTL_EN
bool cfg_bt_rf_ext_ctrl(void) {   //返回true时,蓝牙库中才会回调bb_rf_ext_ctl_cb函数
    return true;
}

//AT(.com_text.dbug_str)
//const char str_btrf_sta[] = "[rf_%X]";

AT(.com_text.isr.txrx)
void bb_rf_ext_ctl_cb(u32 rf_sta) //蓝牙库回调函数
{
    //my_printf(str_btrf_sta,rf_sta);
    //根据TX/RX状态设置IO电平
    //  if(rf_sta & BIT(8)) {           //tx on
    //  } else if(rf_sta & BIT(9)){     //tx down
    //  } else if(rf_sta & BIT(10)){    //rx on
    //  } else if(rf_sta & BIT(11)){    //rx down
    //  } else {                        //idle
    //  }
}
#endif

uint16_t bt_pbap_set_phonebook_count() {    //设置获取的通讯录数量,0为获取全部
    return 0;
}

uint16_t bt_pbap_set_phonebook_offset() {   //设置获取的通讯录偏移量,0为不偏移
    return 0;
}



